<template>
<div class="home">
    <div class="home-left">
        <el-card class="home-card home-card-top" shadow="hover">
            <img class="avatar" :src="require(`@/assets/img/${user.avatar}`)" alt="">
            <div class="home-card-title">
                <span class="title-1">Hi，{{user.username}}，欢迎来到机器学习中心</span>
                <span class="title-2">{{ user.deptName || '暂无部门'}} | {{user.roleName || '暂无角色'}}</span>
                <span class="title-3">上次登录时间：{{user.lastLoginTime || '第一次访问系统'}}</span>
            </div>
            <div class="home-card-info">
                <div>
                    <p class="top-info-title">今日IP</p>
                    <p class="top-info">{{numsInfo.todayIp}}</p>
                </div>
                <div>
                    <p class="top-info-title">今日访问</p>
                    <p class="top-info">{{numsInfo.todayVisitCount}}</p>
                </div>
                <div>
                    <p class="top-info-title">总访问量</p>
                    <p class="top-info">{{numsInfo.totalVisitCount}}</p>
                </div>
            </div>
        </el-card>
        <div class="items home-card">
            <el-card class="item" shadow="hover" v-for="(item, i) in list" :key="i">
                <i :class="item.icon"></i>
                <div>
                    <p class="name">{{item.name}}</p>
                    <p class="num">{{item.num}}</p>
                </div>
            </el-card>
        </div>
        <el-card shadow="hover">
            <div slot="header" class="clearfix">
                <span>任务概况</span>
            </div>
            <ve-line width="100%" :data="chartData1" :settings="chartSettings"></ve-line>
        </el-card>
    </div>
    <div class="home-right">
        <el-card class="home-card" shadow="hover">
            <div slot="header" class="clearfix">
                <span>模型分类</span>
            </div>
            <div class="scroll-bar model-type-box">
                <el-tag v-for="(item, i) in types" :key="i">{{item.name}}</el-tag>
            </div>
        </el-card>
        <el-card shadow="hover">
            <div slot="header" class="clearfix">
                <span>今日调度概览</span>
            </div>
            <ve-pie width="100%" height="300px" :data="chartData2"></ve-pie>
        </el-card>
    </div>
</div>
</template>

<script>
export default {
    name: 'Home',
    data () {
        return {
            types: [
                { name: '逻辑回归二分类' },
                { name: '线性SVM二分类' },
                { name: '随机森林二分类' },
                { name: 'GBDT二分类' },
                { name: '朴素贝叶斯二分类' },
                { name: 'XGBoost二分类' },
                { name: 'LightGBM二分类' },
                { name: '朴素贝叶斯多分类' },
                { name: '逻辑回归多分类' },
                { name: '随机森林多分类' },
                { name: 'K近邻多分类' },
                { name: 'K均值聚类' },
                { name: 'DBSCAN聚类' },
                { name: '线性回归' },
                { name: 'GBDT回归' },
                { name: '贝叶斯线性回归' },
                { name: '泊松回归' },
                { name: '神经网络回归' },
                { name: '协同过滤' },
                { name: 'SVM异常检测' },
                { name: 'PCA异常检测' },
                { name: 'Apriori关联分析' },
                { name: 'FP-Growth关联分析' },
                { name: '模型训练' },
                { name: '模型预测' },
                { name: '模型自动调参' },
                { name: '模型交叉验证' },
                { name: '二分类模型评估' },
                { name: '多分类模型评估' },
                { name: '聚类模型评估' },
                { name: '单分类模型评估' },
                { name: '回归模型评估' },
                { name: '规则生成套件' },
                { name: '评分卡套件' }
            ],
            numsInfo: {
                todayIp: null,
                todayVisitCount: null,
                totalVisitCount: null
            },
            user: JSON.parse(localStorage.getItem('USER')),
            list: [
                { icon: 'iconfont icon-moxingliebiao', name: '项目总数', num: 123 },
                { icon: 'iconfont icon-moxingliebiao', name: '实验总数', num: 23 },
                { icon: 'iconfont icon-moxingliebiao', name: '数据总量', num: 234 },
                { icon: 'iconfont icon-moxingliebiao', name: '模型总数', num: 2345 }
            ],
            chartSettings: {},
            chartData1: {
                columns: ['日期', '访问用户', '下单用户', '下单率'],
                rows: [
                    { '日期': '2018-01-01', '访问用户': 1393, '下单用户': 1093, '下单率': 0.32 },
                    { '日期': '2018-01-02', '访问用户': 3530, '下单用户': 3230, '下单率': 0.26 },
                    { '日期': '2018-01-03', '访问用户': 2923, '下单用户': 2623, '下单率': 0.76 },
                    { '日期': '2018-01-05', '访问用户': 1723, '下单用户': 1423, '下单率': 0.49 },
                    { '日期': '2018-01-10', '访问用户': 3792, '下单用户': 3492, '下单率': 0.323 },
                    { '日期': '2018-01-20', '访问用户': 4593, '下单用户': 4293, '下单率': 0.78 }
                ]
            },
            chartData2: {
                columns: ['日期', '访问用户'],
                rows: [
                    { '日期': '1/1', '访问用户': 1393 },
                    { '日期': '1/2', '访问用户': 3530 },
                    { '日期': '1/3', '访问用户': 2923 },
                    { '日期': '1/4', '访问用户': 1723 },
                    { '日期': '1/5', '访问用户': 3792 },
                    { '日期': '1/6', '访问用户': 4593 }
                ]
                }
        }
    },
    created () {
        this.getNum()
        this.checkIsFirstLogin(this.user.lastLoginTime)
    },
    methods: {
        getNum () {
            this.$get(`index/${this.user.username}`).then(res => {
                if (res) {
                    this.numsInfo = {
                        todayIp: res.todayIp,
                        todayVisitCount: res.todayVisitCount,
                        totalVisitCount: res.totalVisitCount
                    }
                }
            })
        },
        checkIsFirstLogin (lastLoginTime) {
            lastLoginTime === null && !localStorage.getItem('isShowFirstMsg') && this.$message.warning('首次登录请修改登录密码') && localStorage.setItem('isShowFirstMsg', 1)
        }
    }
}
</script>

<style lang="less">
.home {
    padding-bottom: 50px;
    display: flex;
    .model-type-box {
        height: 280px;
        width: 100%;
        .el-tag {
            margin: 5px;
        }
    }
    .home-card-info {
        padding-right: 6%;
        position: absolute;
        right: 20px;
        display: flex;
        div {
            margin-left: 10%;
        }
    }
    .home-card-top {
        .el-card__body {
            position: relative;
            display: flex;
            align-items: center;
            .home-card-title {
                margin-left: 20px;
                display: flex;
                flex-direction: column;
                justify-content: space-between;
                font-size: 14px;
            }
            .title-1 {
                font-size: 16px;
                color: #000;
            }
            .title-2 {
                margin: 5px 0;
            }
            .top-info {
                color: #409EFF;
                text-align: center;
            }
            .top-info-title {
                white-space: nowrap;
            }
        }
    }
    .avatar {
        width: 80px;
        height: 80px;
        border: none;
        border-radius: 2px;
    }
    .home-left {
        width: 65%;
        margin-right: 8px;
    }
    .home-right {
        width: 35%;
    }
    .home-card {
        margin-bottom: 8px;
    }
    .items {
        display: flex;
        align-items: center;
        justify-content: space-between;
        height: 120px;
        .num {
            font-weight: 600;
        }
        .item {
            height: 100%;
            width: 25%;
            margin-right: 8px;
            .name {
                white-space: nowrap;
            }
            .el-card__body {
                height: 100%;
                width: 100%;
                display: flex;
                align-items: center;
                justify-content: center;
                i {
                    font-size: 66px;
                    color: salmon;
                    margin-right: 10%;
                }
            }
        }
        .item:last-child {
            margin-right: 0;
        }
    }
}
</style>
